home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / me_cd25.zip / MUTT2.ZIP / RANDOM.MUT < prev    next >
Text File  |  1992-11-09  |  862b  |  32 lines

  1. ;; Here's a typical (portable) linear-congruential pseudo-random
  2. ;; number generator, taken from the draft ANSI C standard; rand() returns an
  3. ;; integer uniformly distributed from 0 through 32767 (inclusive), and srand()
  4. ;; is used to initialize a sequence to a particular seed, or to avoid getting
  5. ;; a predictable sequence (by setting a seed based on some system variable
  6. ;; such as process ID or time-of-day).
  7.  
  8. ; static unsigned long int next = 1;
  9. ; int rand(void)
  10. ; {
  11. ;   next = next * 1103515245 + 12345;
  12. ;   return (unsigned int)(next/65536) % 32768;
  13. ; }
  14. ; void srand(unsigned int seed) { next = seed; }
  15.  
  16.  
  17. (include mod.mut)
  18. (int next)
  19. (defun
  20.   rand
  21.   {
  22.     (int x)
  23.  
  24.     (next (+ (* next 1103515245) 12345))
  25.     (x (mod (/ next 65536) 32768))
  26.     (if (< x 0) (+ x 32767) x)
  27.   }
  28.   srand (int seed) { (next seed) }
  29. )
  30.  
  31. ;(next 1)
  32.